/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/02/18
* File: enn_vector3.h
**/

#pragma once

#include "enn_math_def.h"
#include "enn_vector2.h"

namespace enn
{
/// Three-dimensional vector.
template <typename T> class Vector3
{
public:
	/// Construct undefined.
	Vector3()
	{
	}

	/// Copy-construct from another vector.
	Vector3(const Vector3<T>& vector) :
	x_(vector.x_),
		y_(vector.y_),
		z_(vector.z_)
	{
	}

	/// Construct from a two-dimensional vector and the Z coordinate.
	Vector3(const Vector2<T>& vector, T z) :
	x_(vector.x_),
		y_(vector.y_),
		z_(z)
	{
	}

	/// Construct from coordinates.
	Vector3(T x, T y, T z) :
	x_(x),
		y_(y),
		z_(z)
	{
	}

	/// Construct from a float array.
	Vector3(const T* data) :
	x_(data[0]),
		y_(data[1]),
		z_(data[2])
	{
	}

	/// Assign from another vector.
	Vector3& operator = (const Vector3<T>& rhs)
	{
		x_ = rhs.x_;
		y_ = rhs.y_;
		z_ = rhs.z_;
		return *this;
	}

	/// Test for equality with another vector without epsilon.
	bool operator == (const Vector3<T>& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
	/// Test for inequality with another vector without epsilon.
	bool operator != (const Vector3<T>& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
	/// Add a vector.
	Vector3<T> operator + (const Vector3<T>& rhs) const { return Vector3<T>(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
	/// Return negation.
	Vector3<T> operator - () const { return Vector3<T>(-x_, -y_, -z_); }
	/// Subtract a vector.
	Vector3<T> operator - (const Vector3<T>& rhs) const { return Vector3<T>(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
	/// Multiply with a scalar.
	Vector3<T> operator * (T rhs) const { return Vector3<T>(x_ * rhs, y_ * rhs, z_ * rhs); }
	/// Multiply with a vector.
	Vector3<T> operator * (const Vector3<T>& rhs) const { return Vector3<T>(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_); }
	/// Divide by a scalar.
	Vector3<T> operator / (T rhs) const { return Vector3<T>(x_ / rhs, y_ / rhs, z_ / rhs); }
	/// Divide by a vector.
	Vector3<T> operator / (const Vector3<T>& rhs) const { return Vector3<T>(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_); }

	/// Add-assign a vector.
	Vector3<T>& operator += (const Vector3<T>& rhs)
	{
		x_ += rhs.x_;
		y_ += rhs.y_;
		z_ += rhs.z_;
		return *this;
	}

	/// Subtract-assign a vector.
	Vector3<T>& operator -= (const Vector3<T>& rhs)
	{
		x_ -= rhs.x_;
		y_ -= rhs.y_;
		z_ -= rhs.z_;
		return *this;
	}

	/// Multiply-assign a scalar.
	Vector3<T>& operator *= (T rhs)
	{
		x_ *= rhs;
		y_ *= rhs;
		z_ *= rhs;
		return *this;
	}

	/// Multiply-assign a vector.
	Vector3<T>& operator *= (const Vector3<T>& rhs)
	{
		x_ *= rhs.x_;
		y_ *= rhs.y_;
		z_ *= rhs.z_;
		return *this;
	}

	/// Divide-assign a scalar.
	Vector3<T>& operator /= (T rhs)
	{
		x_ /= rhs;
		y_ /= rhs;
		z_ /= rhs;
		return *this;
	}

	/// Divide-assign a vector.
	Vector3<T>& operator /= (const Vector3<T>& rhs)
	{
		x_ /= rhs.x_;
		y_ /= rhs.y_;
		z_ /= rhs.z_;
		return *this;
	}

	/// Normalize to unit length and return the previous length.
	T Normalize()
	{
		T len = Length();
		if (len >= ENN_EPSILON)
		{
			x_ /= len;
			y_ /= len;
			z_ /= len;
		}

		return len;
	}

	/// Return length.
	T Length() const { return sqrt(x_ * x_ + y_ * y_ + z_ * z_); }
	/// Return squared length.
	T LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
	/// Calculate dot product.
	T DotProduct(const Vector3<T>& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
	/// Calculate absolute dot product.
	T AbsDotProduct(const Vector3<T>& rhs) const { return enn::Abs(x_ * rhs.x_) + enn::Abs(y_ * rhs.y_) + enn::Abs(z_ * rhs.z_); }

	/// Calculate cross product.
	Vector3<T> CrossProduct(const Vector3<T>& rhs) const
	{
		return Vector3<T>(
			y_ * rhs.z_ - z_ * rhs.y_,
			z_ * rhs.x_ - x_ * rhs.z_,
			x_ * rhs.y_ - y_ * rhs.x_
			);
	}

	/// Return absolute vector.
	Vector3<T> Abs() const { return Vector3<T>(enn::Abs(x_), enn::Abs(y_), enn::Abs(z_)); }
	/// Linear interpolation with another vector.
	Vector3<T> Lerp(const Vector3<T>& rhs, T t) const { return *this * (1.0f - t) + rhs * t; }
	/// Test for equality with another vector with epsilon.
	bool Equals(const Vector3<T>& rhs) const { return enn::Equals(x_, rhs.x_) && enn::Equals(y_, rhs.y_) && enn::Equals(z_, rhs.z_); }

	/// Return normalized to unit length.
	Vector3<T> Normalized() const
	{
		T len = Length();
		if (len >= ENN_EPSILON)
			return *this / len;
		else
			return *this;
	}

	/// Return float data.
	const T* Data() const { return &x_; }

	/// X coordinate.
	T x_;
	/// Y coordinate.
	T y_;
	/// Z coordinate.
	T z_;

	/// Zero vector.
	static const Vector3<T> ZERO;
	/// (-1,0,0) vector.
	static const Vector3<T> LEFT;
	/// (1,0,0) vector.
	static const Vector3<T> RIGHT;
	/// (0,1,0) vector.
	static const Vector3<T> UP;
	/// (0,-1,0) vector.
	static const Vector3<T> DOWN;
	/// (0,0,1) vector.
	static const Vector3<T> FORWARD;
	/// (0,0,-1) vector.
	static const Vector3<T> BACK;
	/// (1,1,1) vector.
	static const Vector3<T> ONE;
};

/// Multiply Vector3 with a scalar.
template <typename T>
inline Vector3<T> operator * (T lhs, const Vector3<T>& rhs) { return rhs * lhs; }

typedef Vector3<float> vec3f;
typedef Vector3<double> vec3d;
typedef Vector3<int> vec3i;
typedef Vector3<unsigned int> vec3ui;
}

